<?php
namespace app\admin\service;

use think\Loader;
use app\admin\service\Common;
use app\admin\model\Stock AS StockModel;
use app\admin\model\ProfitForecast AS ProfitForecastModel;

class ProfitForecast extends Common {

    const PROFIT_FORECASST = 'https://emweb.eastmoney.com/PC_HSF10/ProfitForecast/PageAjax?code=';
    public function _initialize(){
    }

    public static function Collect($stockId = 0, $arr = []){
        $reProfitForecast = "";
        $reProfitForecast = file_get_contents(self::PROFIT_FORECASST.$arr['jys'].$arr['stock_code']);
        if(!empty($reProfitForecast)){
            $reProfitForecast = json_decode($reProfitForecast,true);
p($reProfitForecast,1);
            $year = date('Y');
            $data = [];
            $data['sid'] = $stockId;
            $data['current_year'] = $year;
            //每股收益及增长率
            $data['val_type'] = 'mgsy';
            $data['value'] = sprintf("%01.2f", $reProfitForecast['yctj_chart'][2]['EPS']);
            $data['ratio'] = sprintf("%01.2f", $reProfitForecast['yctj_chart'][2]['EPS_RATIO']);
            $data['year'] = $reProfitForecast['yctj_chart'][2]['YEAR'];
            $data['remark'] = json_encode($reProfitForecast['yctj_chart']);
            $where = [];
            $where = ['sid'=>$stockId, 'val_type'=>$data['val_type'],'year'=>$data['year'],'current_year'=>$data['current_year']];
            if(ProfitForecastModel::Detail($where)){
                ProfitForecastModel::where($where)->update($data);
            }else{
                ProfitForecastModel::insert($data);
            }
            //评级
            $data['val_type'] = 'pj';
            if(!empty($reProfitForecast['pjtj'][4]['COMPRE_RATING']) && !empty($reProfitForecast['pjtj'][4]['COMPRE_RATING_NUM'])){

                $data['value'] = $reProfitForecast['pjtj'][4]['COMPRE_RATING'];
                $data['ratio'] = sprintf("%01.2f", $reProfitForecast['pjtj'][4]['COMPRE_RATING_NUM']);
                $data['year'] = $reProfitForecast['pjtj'][4]['DATE_TYPE'];
                $tzpj = [];
                $tzpj['投资评级']['时间段'] = $data['year'];
                $tzpj['投资评级']['评级系数'] = $data['ratio'];
                $tzpj['投资评级']['综合评级'] = $data['value'];
                $tzpj['评级分布']['买入'] = $reProfitForecast['pjtj'][4]['RATING_BUY_NUM'];
                $tzpj['评级分布']['增持'] = $reProfitForecast['pjtj'][4]['RATING_ADD_NUM'];
                $tzpj['评级分布']['中性'] = $reProfitForecast['pjtj'][4]['RATING_NEUTRAL_NUM'];
                $tzpj['评级分布']['减持'] = $reProfitForecast['pjtj'][4]['RATING_REDUCE_NUM'];
                $tzpj['评级分布']['卖出'] = $reProfitForecast['pjtj'][4]['RATING_SALE_NUM'];
                $data['remark'] = json_encode($tzpj);
            }
            $where = [];
            $where = ['sid'=>$stockId, 'val_type'=>$data['val_type'],'year'=>$data['year'],'current_year'=>$year];
            if(ProfitForecastModel::Detail($where)){
                ProfitForecastModel::where($where)->update($data);
            }else{
                ProfitForecastModel::insert($data);
            }
            //市盈率
            $data['val_type'] = 'pe';
            $data['value'] = sprintf("%01.2f", $reProfitForecast['yctj_chart'][2]['PE']);
            $data['ratio'] = 0;
            $data['year'] = $reProfitForecast['yctj_chart'][2]['YEAR'];
            $data['remark'] = json_encode($reProfitForecast['yctj_chart']);
            $where = [];
            $where = ['sid'=>$stockId, 'val_type'=>$data['val_type'],'year'=>$data['year'],'current_year'=>$data['current_year']];
            if(ProfitForecastModel::Detail($where)){
                ProfitForecastModel::where($where)->update($data);
            }else{
                ProfitForecastModel::insert($data);
            }
            //净资产收益率
            $data['val_type'] = 'roe';
            $data['value'] = sprintf("%01.2f", $reProfitForecast['yctj_chart'][2]['ROE']);
            $data['ratio'] = 0;
            $data['year'] = $reProfitForecast['yctj_chart'][2]['YEAR'];
            $data['remark'] = json_encode($reProfitForecast['yctj_chart']);
            $where = [];
            $where = ['sid'=>$stockId, 'val_type'=>$data['val_type'],'year'=>$data['year'],'current_year'=>$data['current_year']];
            if(ProfitForecastModel::Detail($where)){
                ProfitForecastModel::where($where)->update($data);
            }else{
                ProfitForecastModel::insert($data);
            }
            //归属净利润
            $data['val_type'] = 'gsjlr';
            $data['value'] = sprintf("%01.2f", $reProfitForecast['yctj_chart'][2]['PARENT_NETPROFIT']);
            $data['ratio'] = sprintf("%01.2f", $reProfitForecast['yctj_chart'][2]['PARENT_NETPROFIT_RATIO']);
            $data['year'] = $reProfitForecast['yctj_chart'][2]['YEAR'];
            $data['remark'] = json_encode($reProfitForecast['yctj_chart']);
            $where = [];
            $where = ['sid'=>$stockId, 'val_type'=>$data['val_type'],'year'=>$data['year'],'current_year'=>$data['current_year']];
            if(ProfitForecastModel::Detail($where)){
                ProfitForecastModel::where($where)->update($data);
            }else{
                ProfitForecastModel::insert($data);
            }
            //营业收入
            $data['val_type'] = 'yysr';
            $data['value'] = sprintf("%01.2f", $reProfitForecast['yctj_chart'][2]['TOTAL_OPERATE_INCOME']);
            $data['ratio'] = sprintf("%01.2f", $reProfitForecast['yctj_chart'][2]['TOTAL_OPERATE_INCOME_RATIO']);
            $data['year'] = $reProfitForecast['yctj_chart'][2]['YEAR'];
            $data['remark'] = json_encode($reProfitForecast['yctj_chart']);
            $where = [];
            $where = ['sid'=>$stockId, 'val_type'=>$data['val_type'],'year'=>$data['year'],'current_year'=>$data['current_year']];
            if(ProfitForecastModel::Detail($where)){
                ProfitForecastModel::where($where)->update($data);
            }else{
                ProfitForecastModel::insert($data);
            }
            //每股净资产
            $data['val_type'] = 'mgjzc';
            $data['value'] = sprintf("%01.2f", $reProfitForecast['yctj_list'][2]['EPS']);
            $data['ratio'] = 0;
            $data['year'] = $reProfitForecast['yctj_list'][2]['YEAR'];
            $data['remark'] = json_encode($reProfitForecast['yctj_list']);
            $where = [];
            $where = ['sid'=>$stockId, 'val_type'=>$data['val_type'],'year'=>$data['year'],'current_year'=>$data['current_year']];
            if(ProfitForecastModel::Detail($where)){
                ProfitForecastModel::where($where)->update($data);
            }else{
                ProfitForecastModel::insert($data);
            }
            //每股净资产
            $data['val_type'] = 'mgjzc';
            $data['value'] = sprintf("%01.2f", $reProfitForecast['yctj_list'][2]['EPS']);
            $data['ratio'] = 0;
            $data['year'] = $reProfitForecast['yctj_list'][2]['YEAR'];
            $data['remark'] = json_encode($reProfitForecast['yctj_list']);
            $where = [];
            $where = ['sid'=>$stockId, 'val_type'=>$data['val_type'],'year'=>$data['year'],'current_year'=>$data['current_year']];
            if(ProfitForecastModel::Detail($where)){
                ProfitForecastModel::where($where)->update($data);
            }else{
                ProfitForecastModel::insert($data);
            }
            //归属于母公司股东的净利润
            $data['val_type'] = 'gsymgsgddjlr';
            $data['value'] = sprintf("%01.2f", $reProfitForecast['yctj_list'][2]['PARENT_NETPROFIT']);
            $data['ratio'] = 0;
            $data['year'] = $reProfitForecast['yctj_list'][2]['YEAR'];
            $data['remark'] = json_encode($reProfitForecast['yctj_list']);
            $where = [];
            $where = ['sid'=>$stockId, 'val_type'=>$data['val_type'],'year'=>$data['year'],'current_year'=>$data['current_year']];
            if(ProfitForecastModel::Detail($where)){
                ProfitForecastModel::where($where)->update($data);
            }else{
                ProfitForecastModel::insert($data);
            }
            //营业总收入
            $data['val_type'] = 'yyzsr';
            $data['value'] = sprintf("%01.2f", $reProfitForecast['yctj_list'][2]['TOTAL_OPERATE_INCOME']);
            $data['ratio'] = 0;
            $data['year'] = $reProfitForecast['yctj_list'][2]['YEAR'];
            $data['remark'] = json_encode($reProfitForecast['yctj_list']);
            $where = [];
            $where = ['sid'=>$stockId, 'val_type'=>$data['val_type'],'year'=>$data['year'],'current_year'=>$data['current_year']];
            if(ProfitForecastModel::Detail($where)){
                ProfitForecastModel::where($where)->update($data);
            }else{
                ProfitForecastModel::insert($data);
            }
            //营业利润
            $data['val_type'] = 'yylr';
            $data['value'] = sprintf("%01.2f", $reProfitForecast['yctj_list'][2]['OPERATE_PROFIT']);
            $data['ratio'] = 0;
            $data['year'] = $reProfitForecast['yctj_list'][2]['YEAR'];
            $data['remark'] = json_encode($reProfitForecast['yctj_list']);
            $where = [];
            $where = ['sid'=>$stockId, 'val_type'=>$data['val_type'],'year'=>$data['year'],'current_year'=>$data['current_year']];
            if(ProfitForecastModel::Detail($where)){
                ProfitForecastModel::where($where)->update($data);
            }else{
                ProfitForecastModel::insert($data);
            }
        }
        return 1;
    }
}